package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.CarInfo;
import com.lib.fancycoverflow.FancyCoverFlow;
import com.lib.fancycoverflow.FancyCoverFlowAdapter;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by King on 2016/7/26 0026.
 */
public class ArchiveGarageAdapter extends FancyCoverFlowAdapter {
    private Context mContext;
    private int mItemWidth = 0;
    private int mItemHeight = 0;
    private List<CarInfo> mData = new ArrayList<>();

    public ArchiveGarageAdapter(Context context, List<CarInfo> dataList, int width, int height) {
        mContext = context;
        mData.addAll(dataList);
        mItemWidth = width;
        mItemHeight = height;
    }

    @Override
    public int getCount() {
        //return Integer.MAX_VALUE;
        return mData.size();
    }

    @Override
    public CarInfo getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    class ViewHolder {
        ImageView ivBrandLogo;
        TextView tvBrandName;
    }


    @Override
    public View getCoverFlowItem(final int position, View convertView,ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_archive_garage_fcf, null);
            viewHolder = new ViewHolder();
            viewHolder.ivBrandLogo = (ImageView) convertView.findViewById(R.id.iv_item_brand_logo);
            viewHolder.tvBrandName = (TextView) convertView.findViewById(R.id.tv_item_brand_name);
            convertView.setLayoutParams(new FancyCoverFlow.LayoutParams( mItemWidth, mItemHeight));
            if (convertView != null) {
                convertView.setTag(viewHolder);
            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int index = position % mData.size();
        if (mData.size() > index) {
            CarInfo mCarInfo = mData.get(index);
            if (mCarInfo != null) {
                viewHolder.tvBrandName.setText(mCarInfo.getChName());
                final ViewHolder finalViewHolder = viewHolder;
                x.image().bind(viewHolder.ivBrandLogo, mCarInfo.getImageSrc(), new Callback.CommonCallback<Drawable>() {
                    @Override
                    public void onSuccess(Drawable result) {

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        finalViewHolder.ivBrandLogo.setImageResource(R.mipmap.default_icon);
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        }


        return convertView;
    }

}
